package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.SearchHistory;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.SearchHistoryService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/admin/searchHistory")
@Slf4j
public class SearchHistoryController {
    @Resource
    private SearchHistoryService searchHistoryService;

    @GetMapping
    @LogScope(title = "搜索历史管理模块", content = "历史列表信息")
    public Result select(@RequestParam Integer current){
        PageInfo<SearchHistory> pageInfo=searchHistoryService.selectSearchHistory(current);
        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "搜索历史管理模块", content = "根据id查看历史列表信息")
    public Result detail(@PathVariable String id){
        SearchHistory searchHistory=searchHistoryService.selectById(id);
        return Result.success(searchHistory);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "搜索历史管理模块", content = "根据id删除历史列表信息")
    public Result delete(@PathVariable String id){
        int i = searchHistoryService.deleteById(id);
        return i>0 ? Result.success():Result.error();
    }

    @PutMapping
    @LogScope(title = "搜索历史管理模块", content = "修改历史列表信息")
    public Result edit(@RequestBody SearchHistory searchHistory){
        int i = searchHistoryService.edit(searchHistory);
        return i>0 ? Result.success():Result.error();
    }

    @PostMapping("/save")
    @LogScope(title = "搜索历史管理模块", content = "添加历史列表信息")
    public Result save(@RequestBody SearchHistory searchHistory){
        searchHistory.setId(RandomUtil.getRandomNumber());
        searchHistory.setSearchTime(new Date());
        int i = searchHistoryService.save(searchHistory);
        return i>0 ? Result.success():Result.error();
    }
}
